package com.zHan.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zHan.mapper.EmpMapper;
import com.zHan.pojo.Emp;
import com.zHan.pojo.PageBean;
import com.zHan.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Override
    public boolean deleteByIds(List<Integer> ids) {
        return empMapper.deleteByIds(ids)>0;
    }

    //传统方法
    /*@Override
    public PageBean page(Integer page, Integer pageSize) {
        //起始索引 --- start
        //每页展示记录数 --- pageSize
        Integer start=(page-1)*pageSize;
        //获得列表数据
        List<Emp> empList = empMapper.page(start, pageSize);
        //获得总记录数
        Long count = empMapper.count();
        //封装成pageBean
        PageBean pageBean=new PageBean(count,empList);
        return pageBean;
    }*/

    //使用PageHelper分页插件
    @Override
    public PageBean page(Integer page, Integer pageSize,String name, Short gender, LocalDate begin, LocalDate end) {
        // ***设置分页参数
        PageHelper.startPage(page,pageSize);
        //获得列表数据
        List<Emp> empList = empMapper.page(name, gender, begin, end);
        Page<Emp> pageInfo= (Page<Emp>) empList;
        //获得参数
        long total = pageInfo.getTotal();
        List<Emp> result = pageInfo.getResult();
        //封装成pageBean
        PageBean pageBean=new PageBean(total,result);
        return pageBean;
    }

    @Override
    public boolean insert(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        return empMapper.insert(emp)>0;
    }

    @Override
    public Emp selectById(Integer id) {
        return empMapper.selectById(id);
    }

    @Override
    public boolean update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        return empMapper.update(emp)>0;
    }

    @Override
    public Emp login(Emp emp) {
        return empMapper.selectUsernameAndPassword(emp);
    }

    @Override
    public void deleteByDeptId(Integer DeptId) {
        empMapper.deleteByDeptId(DeptId);
    }

}
